// 与数据库打交道
const {
  create,
  del,
  list,
  get,
  update,
  count,
  listByWhere,
} = require('../../../model/ref_system')

const { Success, SysErr } = require('../../../response/code')
const { get: getReq } = require('../../../util/axios')

const doTest = async (ctx, result) => {
  const url = result.ref_system_url
  if (!url) {
    return ctx.app.emit('error', new SysErr('健康检查url不存在'), ctx)
  }
  let success = false

  const reqTime = new Date()
  // 判断
  try {
    const { status, data } = await getReq(url, {
      timeout: 3000,
    })
    console.log(data, '<< data')
    success = status === 200
  } catch (err) {
    console.log(err, '<<< run error')
    success = false
  } finally {
    console.log(success, '<<< success')
    return await update(result.id, {
      last_result: success,
      last_check_time: reqTime,
    })
  }
}

class ref_systemCheckAdminService {
  async create(ctx) {
    try {
      const data = ctx.request.body
      const result = await create(data)
      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async update(ctx) {
    try {
      const data = ctx.request.body
      const result = await update(data.id, data)
      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async get(ctx) {
    try {
      const query = ctx.request.query
      const result = await get(query.id)
      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async del(ctx) {
    try {
      const body = ctx.request.body

      // 存在则删除
      const result = await del(body.id, {
        deleted_at: null,
      })
      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async list(ctx) {
    try {
      const query = ctx.request.query
      const where = {
        deleted_at: null,
      }
      if (query.sys_name && query.sys_name.trim() !== ''){
        where.sys_name = {
          contains: query.sys_name,
        }
      }

     
      // console.log(where, '<<< where')
      const data = await list(query.pageNo, query.pageSize, where) // 可以添加第三个参数条件过滤
      const total = await count(where)
      const result = {
        list: data,
        count: total,
      }

      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async run(ctx) {
    try {
      const data = ctx.request.body
      const result = await get(data.id)
      if (!result) {
        return ctx.app.emit('error', new SysErr('健康检查不存在'), ctx)
      }
      const resp = await doTest(ctx, result)
      // console.log(resp, '<<< run')
      ctx.body = new Success(resp).toString()
      return
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async runAll(ctx) {
    try {
      const list = await listByWhere({
        deleted_at: null,
        enabled: true,
      })
      let result = []

      for (const item of list) {
        const resp = await doTest(ctx, item)
        result.push(resp)
      }

      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }
}
module.exports = new ref_systemCheckAdminService()
